var map=null,clickStatus=false;
var BaiDuMap = {
		/**
		 * 初始化地图
		 */
		map:function(mapId,obj){
			map = new BMap.Map(mapId,obj);
		},
		/**
		 * 定义点
		 * longitude 经度
		 * latitude 纬度
		 */
		point:function(longitude,latitude){
			var point = new BMap.Point(longitude, latitude);
			return point;
		},
		initialize:function (longitude,latitude,zoom){
			// 百度地图API功能
			var point = BaiDuMap.point(longitude, latitude);
			map.centerAndZoom(point, zoom);
			//map.setZoom(zoom); 
			map.enableScrollWheelZoom(true);
			//单击获取点击的经纬度
			map.addEventListener("click",function(e){
				BaiDuMap.getLongLat(e.point.lng,e.point.lat);
			});
		},
		//弹出详细信息框
		popMenu:function (devCode){
			var allOverlay = map.getOverlays();
			for (var i = 0; i < allOverlay.length; i++){
				if(allOverlay[i].devCode == devCode){
					allOverlay[i].V.click();	
					break;
				}
			}		
		},
		deletePoint:function(devCode){
			var allOverlay = map.getOverlays();
			var arrLength =  allOverlay.length;
			for (var i = 0; i < arrLength; i++){
				if(allOverlay[i].devCode == devCode){
					map.removeOverlay(allOverlay[i]);	
				}
			}
			
		},
		/**
		 * 获取点击的经纬度
		 */
		getLongLat:function(longitude,latitude){
			if(clickStatus){
				winTip.alert("经度:"+longitude + ",纬度:" + latitude);
			}
		},
		/**
		 * 允许点击
		 */
		openClick:function(){
			clickStatus = true;
		},
		/**
		 * 禁止点击
		 */
		closeClick:function(){
			clickStatus = false;
		},
		test:function(){
			var myDate = new Date();
			winTip.alert("开始时间:"+myDate.toLocaleTimeString());
			var bounds = map.getBounds();
			var sw = bounds.getSouthWest();
			var ne = bounds.getNorthEast();
			var lngSpan = Math.abs(sw.lng - ne.lng);
			var latSpan = Math.abs(ne.lat - sw.lat);
			for (var i = 0; i < 500; i ++) {
				var point1 = new BMap.Point(sw.lng + lngSpan * (Math.random() * 0.7), ne.lat - latSpan * (Math.random() * 0.7));
				BaiDuMap.addMarker(point1,i,'设备'+i,'杭州市','');
			}
			var myDate1 = new Date();
			winTip.alert("结束时间:"+myDate1.toLocaleTimeString());
		},
		//移动地图
		panTo:function (longitude,latitude){
			map.panTo(BaiDuMap.point(longitude,latitude));  
		},
		
		//添加点击事件 弹出信息框
		addClickHandler:function (content,marker,opts){
		    marker.addEventListener("click",function(e){
		    	BaiDuMap.openInfo(content,e,opts)
		        
		    });
		},
		openInfo:function (content,e,opts){
		    var p = e.target;
		    var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
		    var infoWindow = new BMap.InfoWindow(content,opts); // 创建信息窗口对象
		    map.openInfoWindow(infoWindow,point); //开启信息窗口
		}

};

$(function(){
	/*BaiDuMap.initialize('allmap','120.182701','30.223314',20);*/
	BaiDuMap.map("allmap",{minZoom:8,maxZoom:20});
});